package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.PopwindowInvoicingCompanyBinding;
import com.sgb.kjwl.model.entity.transport.CompanyInvoiceEntity;
import com.sgb.kjwl.utils.DefaultUtil;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 杨畔
 * @date: 3/1/21 9:40 AM
 * @Description: 开票公司弹窗
 */
public class InvoicingCompanyPopWindow extends BasePopWindow implements View.OnClickListener {

    private PopwindowInvoicingCompanyBinding mBinding;
    //
    private CompanyAdapter mAdapter;
    private List<CompanyInvoiceEntity> mList;


    public InvoicingCompanyPopWindow(Context context) {
        super(context, DIR_DOWN_UP);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_invoicing_company;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (PopwindowInvoicingCompanyBinding) holder.getmBinding();
        initRecycler();
        initEvent();
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new CompanyAdapter(mList);
        mBinding.rvList.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter.bindToRecyclerView(mBinding.rvList);
        //设置空布局
        mAdapter.setEmptyView(R.layout.invoicing_company_null);
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            mAdapter.setSelectIndex(position);
            mAdapter.notifyDataSetChanged();
        });
    }

    public void setData(List<CompanyInvoiceEntity> list) {
        mList.clear();
        if (list != null) {
            mList.addAll(list);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v == mBinding.tvCancel) {
            dissMiss();
        } else if (v == mBinding.tvSure) {
            if (mList == null ||mList.size() == 0) {//无数据时点击确定关闭窗口
                dismiss();
                return;
            }
            CompanyInvoiceEntity entity = mAdapter.getSelectedData();
            if (entity == null) {
                MToast.showToast(mContext, "请选择开票信息");
                return;
            }
            onResultClick.result(mAdapter.getSelectedData(), null, null);
            dissMiss();
        }
    }

    private static class CompanyAdapter extends BaseQuickAdapter<CompanyInvoiceEntity, BaseViewHolder> {
        //选中下标
        private int mSelectIndex = -1;

        public CompanyAdapter(@Nullable List<CompanyInvoiceEntity> data) {
            super(R.layout.item_invoicing_company, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, CompanyInvoiceEntity item) {
            helper.setText(R.id.tv_title, DefaultUtil.getString(item.getCompanyName()))
                    .setText(R.id.tv_content, DefaultUtil.getString(item.getCreditCode()))
                    .setVisible(R.id.iv_select, helper.getLayoutPosition() == mSelectIndex)
                    .getView(R.id.ll_content).setSelected(helper.getLayoutPosition() == mSelectIndex);
        }

        /**
         * 设置选中下标
         */
        public void setSelectIndex(int mSelectIndex) {
            this.mSelectIndex = mSelectIndex;
        }

        /**
         * 获取选中的数据
         */
        public CompanyInvoiceEntity getSelectedData() {
            return mSelectIndex < 0 ? null : mData.get(mSelectIndex);
        }
    }
}
